<?php
namespace App\Libs\Views;

class Twig extends View
{
    private $engine;
    public function __construct() {
		$loader = new \Twig_Loader_Filesystem(APP_PATH . "app/views/tpl");
		$this->engine = new \Twig_Environment($loader, array(
		    'cache' => APP_PATH . "public/templates_c",
			'debug' => true,
			));

    }
    public function show($response) {
        if (is_array($response)) {
            $params = isset($response['params']) ? $response['params'] : array();
            $tpl = isset($response['tpl']) && isset($response['tpl']) ? trim($response['tpl']) : 'empty.tpl.html';
            $base = isset($response['base']) && isset($response['base']) ? trim($response['base']) : 'html/base/base.tpl.html';
        } else {
            $params = array();
            $tpl = 'empty.tpl.html';
            $base = 'html/base/base.tpl.html';
        }
        $r = $this->engine->render($tpl, $params);
		echo $r;
    }

    public function fetch($response) {
        if (is_array($response)) {
            $params = isset($response['params']) ? $response['params'] : array();
            $tpl = isset($response['tpl']) && isset($response['tpl']) ? trim($response['tpl']) : 'empty.tpl.html';
        } else {
        }
		return $this->engine->render($tpl, $params);
    }

    public static function view($params, $tpl, $base = '') {
        return array('params' => $params, 'tpl' => $tpl, 'base' => $base);
    }
}
